<!DOCTYPE HTML>
<html>
<head>
    <title>授权管理</title>
    <script type="text/javascript" src="../js/common.js"></script>
    <link href="../js/bootstrap-treeview/bootstrap-treeview.css" rel="stylesheet" type="text/css"/>
    <script type="text/javascript" src="../js/bootstrap-treeview/bootstrap-treeview.js"></script>
</head>
<body>
<div class="pd-20">
    <div class="panel-body" style="padding-bottom:0px;">
        <div class="panel panel-default">
            <div class="panel-heading">查询条件</div>
            <div class="panel-body">
                <div class="form-group" style="margin-top:15px">
                    <label class="control-label col-sm-4">菜单名称:</label>
                    <div class="col-sm-4">
                        <input id="search" type="text" class="form-control">
                    </div>
                    <div class="col-sm-4" style="text-align:left;">
                        <button type="button" class="btn btn-info" onclick="search()"><i
                                class="glyphicon glyphicon-search"></i>搜索
                        </button>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <div id="toolbar" class="btn-group">
        <button id="btn_add" type="button" class="btn btn-default"
                onclick="save()">
            <span class="glyphicon glyphicon-save" aria-hidden="true"></span>保存
        </button>
    </div>
    <table id="tree"></table>
</div>

<script type="text/javascript">
    var id = getQueryVariable("id");
    var row = JSON.parse(window.sessionStorage.getItem("row"));
    initTree();
    //初始化菜单树
    function initTree() {
        $('#tree').treeview({
            data: getTree(),
            levels: 0,
            showTags: true,
            showCheckbox: true
        });
    }

    //获取原数据
    function getTree() {
        var menu_data;
        requestService("/permission/role/" + id, "get", false, "", function (result) {
            $("#size").text(result.data.length);
            menu_data = dataFormatTree(result.data, null);
        }, null);
        return menu_data;
    }

    //格式化数据(json树结构)
    function dataFormatTree(permissions, p_id) {
        var menu = [];
        for (var i = 0; i < permissions.length; i++) {
            var permission = permissions[i];
            if (permission.p_id == p_id) {
                var checked = false;
                if (permission.role_id != null) {
                    checked = true;
                }
                var newNode = {
                    id: permission.id,
                    text: permission.permission_name,
                    state: {expanded: true, checked: checked},
                    nodes: dataFormatTree(permissions, permission.id)
                };
                menu.push(newNode);
                /*permissions.splice(i, 1);
                 i--;*/
            }
        }
        return menu;
    }

    function search() {
        var text = $("#search").val();
        $('#tree').treeview('search', [text, {
            ignoreCase: true,     // case insensitive
            exactMatch: false,    // like or equals
            revealResults: true  // reveal matching nodes
        }]);
    }

    /**
     * 保存授权信息
     */
    function save() {
        var rows = $('#tree').treeview('getChecked');
        var permission_ids = [];
        $.each(rows, function (i, r) {
            permission_ids.push(r.id);
        });
        requestService("/role/savePermission", "post", true, {
            permission_ids: permission_ids,
            role_id: id
        }, function (result) {
            msgAndAfter(result.desc, function () {
                initTree();
            }, 500);
        }, null);
    }
</script>
</body>
</html>